<?php

namespace App\Admin\Controllers\goods;

use App\Common\statement\CommonStatement;
use App\Models\goods\Goods;
use App\Models\goods\GoodsCate;
use App\Services\common\App;
use App\Services\goods\GoodsService;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Http\Request;

class GoodsController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '产品';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Goods);
        $grid->model()->orderBy('sort');
        $grid->disableExport();
        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
//            $filter->column(1/3, function ($filter) {
////
////                $filter->like( 'spu_id',__('商品编号'));
////
////            });
            $filter->column(1/3, function ($filter) {

                $filter->like( 'name',__('Name'));

            });

            $filter->column(1/3, function ($filter) {
                $filter->between('created_at',__('Created at'))->datetime();
            });

        });

        $grid->column('id', __('Id'));
//        $grid->column('spu_id', __('商品编号'));
        $grid->column('img', __('Img'))
            ->lightbox(['zooming' => true,'width' => 50, 'height' => 50, 'class' => ['circle', 'thumbnail']]);
        $grid->column('name', __('Name'));
//        $grid->column('cate_id', __('Cate id'));
//        $grid->column('brand_id', __('Brand id'));
//        $grid->column('sale_num', __('Sale num'));


        $grid->column('price', __('Price'));
        $grid->column('stock', __('Stock'))->editable();
        $grid->column('status', __('Status'))->switch(CommonStatement::goods_switch);
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Goods::findOrFail($id));

        $show->field('id', __('Id'));
//        $show->field('spu_id', __('商品编号'));
        $show->field('name', __('Name'));
//        $show->field('cate_id', __('Cate id'));
//        $show->field('brand_id', __('Brand id'));
//        $show->field('sale_num', __('Sale num'));
        $show->field('price', __('Price'));
//        $show->field('old_price', __('Old price'));
        $show->field('stock', __('Stock'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $model = new Goods;

        $goods = $model->find(\request()->good);
        $form = new Form($model);

        $form->column(1/2, function (\Encore\Admin\Form $form)use ($goods) {
            $form->image('img', __('Img'))->help("尺寸:800*800px")->required();
            $form->text('name', __('Name'))->required();
            $form->decimal('price', __('Price'))->required();
            $form->number('stock', __('Stock'))->required()->default(0);
            $form->switch('status', __('Status'))
                ->states(CommonStatement::goods_switch)
                ->default(CommonStatement::default_status);
        });
        return $form;
    }
}
